﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

using Sabnzbd;
using Bierdopje.API;

namespace Bierdopje.RSS
{
    public class BierdopjeFeed
    {
        private List<BierdopjeFeedItem> m_FeedItems;
        private Bierdopje.API.ApiLibrary m_Api;

        public Bierdopje.API.ApiLibrary Api
        {
            get { return m_Api; }
            set { m_Api = value; }
        }

        public BierdopjeFeed(string url, ApiLibrary apiLibrary)
        {
            m_Api = apiLibrary;
            Construct(url);
        }

        public BierdopjeFeed(string url)
        {
            Construct(url);
        }

        private void Construct(string url)
        {
            string _StringFeed = HTTPHelper.GetHTTPResponse(url);
            _StringFeed = XMLHelper.XMLCleanup(_StringFeed);

            m_FeedItems = new List<BierdopjeFeedItem>();

            XmlDocument _FeedXml = new XmlDocument();
            _FeedXml.LoadXml(_StringFeed);

            foreach (XmlNode _ItemNode in _FeedXml.SelectNodes(@"//item"))
            {
                try
                {
                    BierdopjeFeedItem _BFI;
                    if (m_Api != null)
                    {
                        _BFI = new BierdopjeFeedItem(m_Api);
                    }
                    else
                    {
                        _BFI = new BierdopjeFeedItem();
                    }
                    _BFI.Description = _ItemNode.SelectSingleNode("description").InnerText.Trim();
                    _BFI.Episode = Convert.ToInt32(_ItemNode.SelectSingleNode("episode").InnerText.Trim());
                    //_BFI.Link = _ItemNode.SelectSingleNode("link").InnerText.Trim();
                    _BFI.Link = _ItemNode.SelectSingleNode("enclosure").Attributes["url"].Value.Trim();
                    _BFI.Season = Convert.ToInt32(_ItemNode.SelectSingleNode("season").InnerText.Trim());
                    _BFI.ShowID = Convert.ToInt32(_ItemNode.SelectSingleNode("show_id").InnerText.Trim());
                    _BFI.ShowName = _ItemNode.SelectSingleNode("showname").InnerText.Trim();
                    _BFI.Size = Convert.ToInt32(_ItemNode.SelectSingleNode("enclosure").Attributes["length"].Value.Trim());
                    _BFI.Title = _ItemNode.SelectSingleNode("title").InnerText.Trim();
                    _BFI.Uploader = _ItemNode.SelectSingleNode("uploader").InnerText.Trim();
                    m_FeedItems.Add(_BFI);
                }
                catch { }
            }
        }

        public List<BierdopjeFeedItem> FeedItems
        {
            get { return m_FeedItems; }
        }

        public List<BierdopjeFeedItem> GetMatchingFeedItems(List<szdFeed> feeds)
        {
            List<BierdopjeFeedItem> _ReturnFeedItems = new List<BierdopjeFeedItem>();
            
            foreach (BierdopjeFeedItem _BFI in m_FeedItems)
            {
                bool Matched = false;
                bool Reject = false;
                szdFilter _MatchedFilter = null;
                foreach (szdFeed feed in feeds)
                {
                    
                    foreach (szdFilter filter in feed.Filters)
                    {
                        if (!filter.SearchString.Equals("*"))
                        {
                            if (TextHelper.WildcardTextCompare(filter.SearchString, _BFI.Link, false))
                            {
                                if (filter.FilterType == szdFilterType.Accept)
                                {
                                    Matched = true;
                                    _MatchedFilter = filter;
                                    break;
                                }
                                else if (filter.FilterType == szdFilterType.Reject)
                                {
                                    Reject = true;
                                    break;
                                }
                            }
                            else if (filter.FilterType == szdFilterType.Must)
                            {
                                Reject = true;
                                break;
                            }
                        }
                    }
                    if (Matched || Reject)
                        break;
                }

                if (Matched && !Reject)
                {
                    _BFI.MatchedFilter = _MatchedFilter;
                    _ReturnFeedItems.Add(_BFI);
                }
            }

            return _ReturnFeedItems;
        }
    }
}
